#第四到六节的内容


import sys
from base.CommonWin import BaseMainWin
from PyQt5.QtWidgets import (QApplication,QAction,qApp,
QFileDialog,QLabel,QTextBrowser,
QPushButton,QLineEdit,QInputDialog,QColorDialog,QFrame,QFontDialog)
from PyQt5.QtGui import QIcon,QKeySequence,QMouseEvent,QKeyEvent,QColor
from PyQt5.QtCore import pyqtSignal,QObject,Qt

#信号槽

class Communicate(QObject):
    triggered = pyqtSignal() #事件信号源 相当于as3的eventtdispatcher

class MyMainWin(BaseMainWin):
    def __init__(self,title='我的QT', sizeW=600, sizeH=600, icoUrl='assets/ico/logo.png'):
        super().__init__(title,sizeW,sizeH,icoUrl)
        self.c = Communicate()

    #设置状态和菜单栏
    def initUI(self):
        super().initUI()
        col = QColor(0,0,0)
        self.frm = QFrame(self)
        self.frm.setStyleSheet("QWidget{ background-color:%s}"%col.name())

        #定义菜单动作
        exitAction = QAction(QIcon('assets/ico/exit.png'),'&退出',self)
        exitAction.setShortcut('ctrl+q')
        exitAction.setStatusTip('退出程序')
        exitAction.triggered.connect(qApp.quit)

        importAction = QAction(QIcon('assets/ico/import.png'),'&打开',self)
        importAction.setShortcut('ctrl+o')
        importAction.setStatusTip('打开文件')
        importAction.triggered.connect(self.openFile)

        self.statusBar()

        #创建菜单
        menuBar = self.menuBar()
        #添加菜单
        fileMenu = menuBar.addMenu('&文件')        
        fileMenu.addAction(exitAction) #为菜单添加动作
        fileMenu.addAction(importAction)

        editMenu = menuBar.addMenu('&编辑')
        
        #工具栏
        self.toolbar = self.addToolBar('退出')
        self.toolbar.addAction(exitAction)
        self.toolbar.setHidden(False) #设置组件显示或者隐藏 都是这个方法
        
        lbl = QLabel('内容',self)
        lbl.move(15,30 + self.toolbar.height())
        lbl.setText("这是一个label组件")

        btn = QPushButton('打开对话框',self)
        btn.move(lbl.x(),lbl.y()+lbl.height())
        btn.clicked.connect(self.showFontDialog)

        self.le = QLineEdit('你好吗',self)
        self.le.move(btn.x(),btn.y()+btn.height())


        
    def showInpDialog(self):
        '''
        显示一个输入对话框
        '''
        text,ok = QInputDialog.getText(self,'输入内容','输入你的名字')
        if ok:
            self.le.setText(text)

    def showClrDialog(self):
        '''
        显示颜色选择对话框
        '''
        col = QColorDialog.getColor()
        if col.isValid():
            self.frm.setStyleSheet("QWidget{ background-color:%s}"%col.name())
            #self.setStyleSheet("QWidget{ background-color:%s}"%col.name())
    def showFontDialog(self):
        '''
        选择字体的对话框
        '''
        font,ok = QFontDialog.getFont()
        if ok:
            self.le.setFont(font)


    #pyqt浏览文件的方法
    def openFile(self):
        sender = self.sender() #获取事件的发送者
        fileName,fileType = QFileDialog.getOpenFileName(self,'选择要打开的文件',
        '','All file(*);;Python(*.py);;Image(*.png)') #参数分别是:父级窗口,对话框标题,文件类型过滤器
        print('fileName,fileType',fileName,fileType,'sender=',sender) #得到完整的文件路径和选择的文件类型字符串
    
    #重新父类的事件监听函数 

    def mousePressEvent(self,event):
        event = QMouseEvent(event)
        print('mousePressEvent event',event,event.windowPos())
        
    def keyPressEvent(self,event):
        print('keyPressEvent event',event,event.key(),Qt.Key_PageDown)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MyMainWin()
    w.show()
    sys.exit(app.exec_())
